﻿using Elight.Entity.Ex;
using System;
using System.Collections.Generic;
using System.Text;

namespace Elight.Logic.Ex
{
    /// <summary>
    /// excel
    /// </summary>
    public class ExExcelLogic : BaseLogic
    {
        /// <summary>
        /// 查询excel列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="keyWord"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<ExExcel> GetList(int pageIndex, int pageSize, string keyWord, ref int totalCount)
        {
            using (var db = GetInstance())
            {
                totalCount = db.Queryable<ExExcel>().WhereIF(!string.IsNullOrEmpty(keyWord), it => it.Name.Contains(keyWord)).Count();
                return db.Queryable<ExExcel>().WhereIF(!string.IsNullOrEmpty(keyWord), it => it.Name.Contains(keyWord)).ToPageList(pageIndex, pageSize);
            }
        }

        /// <summary>
        /// 获取单条信息
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public ExExcel GetForm(string primaryKey)
        {
            using (var db = GetInstance())
            {
                return db.Queryable<ExExcel>().Where(it => it.Id == primaryKey).First();
            }
        }

        public object Insert(ExExcel model)
        {
            using (var db = GetInstance())
            {
                model.Id = Guid.NewGuid().ToString().Replace("-", "");
                return db.Insertable<ExExcel>(model).ExecuteCommand();
            }
        }

        public int Update(ExExcel model)
        {
            using (var db = GetInstance())
            {

                return db.Updateable<ExExcel>(model).UpdateColumns(it => new
                {
                    it.Name,
                    it.Age,
                    it.Price
                }).ExecuteCommand();
            }
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="primaryKeys"></param>
        /// <returns></returns>
        public int Delete(params string[] primaryKeys)
        {
            using (var db = GetInstance())
            {
                try
                {
                    db.Ado.BeginTran();
                    foreach (string primaryKey in primaryKeys)
                    {
                        db.Deleteable<ExExcel>().Where(it => it.Id == primaryKey).ExecuteCommand();
                    }
                    db.Ado.CommitTran();
                    return 1;
                }
                catch (Exception ex)
                {
                    db.Ado.RollbackTran();
                    return 0;
                }
            }
        }
    }
}
